package com.googlecode.horncomb.web.extremetable.custom;

import java.util.Iterator;

import org.extremecomponents.table.bean.Export;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.table.view.html.TwoColumnTableLayout;
import org.extremecomponents.util.HtmlBuilder;

public class LinkToolbar extends TwoColumnTableLayout
{
    public LinkToolbar(HtmlBuilder html, TableModel model)
    {
        super(html, model);
    }

    protected boolean showLayout(TableModel model)
    {
        boolean showStatusBar = BuilderUtils.showStatusBar(model);
        boolean filterable = BuilderUtils.filterable(model);
        boolean showExports = BuilderUtils.showExports(model);
        boolean showPagination = BuilderUtils.showPagination(model);
        boolean showTitle = BuilderUtils.showTitle(model);
        if (!showStatusBar && !filterable && !showExports && !showPagination
                && !showTitle)
        {
            return false;
        }

        return true;
    }

    protected void columnLeft(HtmlBuilder html, TableModel model)
    {
        // boolean showStatusBar = BuilderUtils.showStatusBar(model);
        // if (!showStatusBar)
        // {
        // return;
        // }
        // html.td(4).styleClass(BuilderConstants.STATUS_BAR_CSS).close();
        // new StatusBarBuilder(html, model).statusMessage();
        // html.tdEnd();

        html.td(2).close();
        // html.append("<div id=").append("></div>");
        html.append("<script>var tbItem = document.getElementById('").append(
                model.getTableHandler().prefixWithTableId()).append("bs');")
                .append("if(tbItem){")
                .append("document.write(tbItem.innerHTML);").append(
                        "tbItem.outerHTML='';").append(
                        "tbItem = null;}</script>");
        html.tdEnd();
    }

    @SuppressWarnings("unchecked")
    protected void columnRight(HtmlBuilder html, TableModel model)
    {
        // 显示过滤器
        boolean filterable = BuilderUtils.filterable(model);
        // 显示分页
        boolean showPagination = BuilderUtils.showPagination(model);
        // 显示导出
        boolean showExports = BuilderUtils.showExports(model);

        LinkToolbarBuilder toolbarBuilder = new LinkToolbarBuilder(html, model);

        html.td(4).styleClass(BuilderConstants.COMPACT_TOOLBAR_CSS).align(
                "right").close();

        html.table(4).border("0").cellPadding("1").cellSpacing("2").close();
        html.tr(5).close();

        if (showPagination)
        {
            // 当前页码
            int page = model.getLimit().getPage();
            int totalPages = BuilderUtils.getTotalPages(model);

            html.td(5).close();
            // 首页
            if (BuilderUtils.isFirstPageEnabled(page))
            {
                toolbarBuilder.firstPageItemAsText();
                html.nbsp().nbsp();
            }

            // 上一页
            if (BuilderUtils.isPrevPageEnabled(page))
            {
                toolbarBuilder.prevPageItemAsText();
                html.nbsp().nbsp();
            }

            // 下一页
            if (BuilderUtils.isNextPageEnabled(page, totalPages))
            {
                toolbarBuilder.nextPageItemAsText();
                html.nbsp().nbsp();
            }

            // 末页
            if (BuilderUtils.isLastPageEnabled(page, totalPages))
            {
                toolbarBuilder.lastPageItemAsText();
                html.nbsp().nbsp();
            }

            // toolbar.text.description=当前页:{0} 共{1}页 共{2}条记录
            if (model.getLimit().getTotalRows() == 0)
            {
                html.append(model.getMessages().getMessage(
                        BuilderConstants.STATUSBAR_NO_RESULTS_FOUND));
            }
            else
            {
                Integer total = new Integer(model.getLimit().getTotalRows());
                // Integer from = new Integer(model.getLimit().getRowStart() +
                // 1);
                // Integer to = new Integer(model.getLimit().getRowEnd());
                Object[] messageArguments = { page, totalPages, total };
                html.append(model.getMessages().getMessage(
                        "toolbar.text.description", messageArguments));
            }
            html.nbsp().nbsp();
            // toolbarBuilder.separator();

            // 转到
            // goto value
            String form = BuilderUtils.getForm(model);
            StringBuffer toPage = new StringBuffer();
            toPage.append("document.forms.").append(form).append(".");
            toPage.append(model.getTableHandler().prefixWithTableId()).append(
                    "goto").append(".value");
            // action
            StringBuffer action = new StringBuffer("javascript:");
            action.append((new TableActions(model))
                    .getClearedExportTableIdParameters());
            // append 'p'
            action.append("document.forms.").append(form).append(".");
            action.append(model.getTableHandler().prefixWithTableId()).append(
                    TableConstants.PAGE);
            action.append(".value=").append(toPage.toString()).append(";");

            action.append((new TableActions(model)).getOnInvokeAction());
            // button
            html.append("转到").input("text").name(
                    model.getTableHandler().prefixWithTableId() + "goto").size(
                    "1").styleClass("goPage").close().append("页");
            html.button().onclick(action.toString()).styleClass("goButton")
                    .close().append("go").buttonEnd();
            html.nbsp().nbsp();

            // 每页显示行数
            toolbarBuilder.rowsDisplayedDroplist();

            if (showExports)
            {
                html.nbsp().nbsp();
                toolbarBuilder.separator();
            }
            html.tdEnd();
        }

        if (showExports)
        {
            Iterator iterator = model.getExportHandler().getExports()
                    .iterator();
            for (Iterator iter = iterator; iter.hasNext();)
            {
                html.td(5).close();
                Export export = (Export) iter.next();
                toolbarBuilder.exportItemAsImage(export);
                html.tdEnd();
            }
        }

        if (filterable)
        {
            if (showExports || showPagination)
            {
                html.td(5).close();
                toolbarBuilder.separator();
                html.tdEnd();
            }

            html.td(5).close();
            toolbarBuilder.filterItemAsImage();
            html.tdEnd();

            html.td(5).close();
            toolbarBuilder.clearItemAsImage();
            html.tdEnd();
        }

        html.trEnd(5);

        html.tableEnd(4);

        html.tdEnd();
    }
}
